package com.czl.module_service.adapter

import android.os.Bundle
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.PaymentData
import com.czl.base.util.ToastHelper.showNormalToast
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemHousesPaymentBinding
import com.czl.module_service.fragment.HousesPaymentListFragment
import com.google.gson.JsonArray
import java.util.*

class HousesPaymentListAdapter(private val fragment: HousesPaymentListFragment) :
    BaseQuickAdapter<PaymentData, BaseDataBindingHolder<ItemHousesPaymentBinding>>(
        R.layout.item_houses_payment
    ) {

    override fun convert(
        holder: BaseDataBindingHolder<ItemHousesPaymentBinding>,
        item: PaymentData,
    ) {
        item.payableDateShortStr = TimeUtils.date2String(Date(item.payableDate!!), "yyyy-MM")
        item.payableDateStr = TimeUtils.date2String(Date(item.payableDate!!), "yyyy-MM-dd")
        item.billDateStr = TimeUtils.date2String(Date(item.billDate!!), "yyyy-MM-dd")
        item.billEndDateStr = TimeUtils.date2String(Date(item.billEndDate!!), "yyyy-MM-dd")
        if (item.payStatus == 0 && item.payableMoney?.toDouble()!! >= 0) {
            item.isPay = 1
        } else if (item.payStatus == 0 && item.payableMoney?.toDouble()!! < 0) {
            item.isPay = 2
        } else if (item.payStatus == 1 && item.payableMoney?.toDouble()!! >= 0) {
            item.isPay = 3
        } else if (item.payStatus == 1 && item.payableMoney?.toDouble()!! < 0) {
            item.isPay = 4
        }
        item.payableMoney = String.format("%.2f", item.payableMoney?.toDouble())
        holder.dataBinding?.run {
            data = item
            adapter = this@HousesPaymentListAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is PaymentData) {
            fragment.startContainerActivity(AppConstants.Router.Service.F_HOUSES_PAYMENT_DETAIL_FRAGMENT, Bundle().apply {
                putSerializable("billId", it.billId)
            })
        }
    })

    val goToPayClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is PaymentData) {
            fragment.goToPay(it)
        }
    })
}